package Homework;
import java.util.LinkedList;
import java.util.Stack;
public class Test_evalRPN {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] str = new String[]{"2","1","+","3","*"};
        int num = solution.evalRPN(str);
        System.out.println(num);
    }
}
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i< tokens.length; i++){
            String tmp = tokens[i];
            if(!isOperations(tmp)){
                stack.push(Integer.parseInt(tmp));
            }else {
                Integer val1 = stack.pop();
                Integer val2 = stack.pop();
                switch (tmp) {
                    case "+":
                        stack.push(val2 + val1);
                        break;
                    case "-":
                        stack.push(val2 - val1);
                        break;
                    case "*":
                        stack.push(val2 * val1);
                        break;
                    case "/":
                        stack.push(val2 / val1);
                        break;
                }
            }
        }
        return stack.peek();
    }
    public Boolean isOperations(String t){
        if(t.equals("+") || t.equals("-") || t.equals("*") || t.equals("/")){
            return true;
        }
        return false;
    }
}